#! /bin/sh

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" && fws="${FW_ROOT}/share/fw/sh/fw-find.sh"
test -f "fw/sh/fw-find.sh" && fws="fw/sh/fw-find.sh"
test -f "fw.local/sh/fw-find.sh" && fws="fw.local/sh/fw-find.sh"
test ! -z "$fws" || {
  echo "package/rpm/make-hook: fatal: can't locate fw-find.sh" 1>&2
  exit 1
}

. "$fws"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "package/rpm/make-hook" "sh/fatal.sh"
fw_source "package/rpm/make-hook" "sh/fw-exec.sh"
fw_source "package/rpm/make-hook" "sh/parse-args.sh"
fw_source "package/rpm/make-hook" "sh/validate-args.sh"

comparescript="
rpm_compare () { 
  perl -e '
    sub rpmvercmp (\$\$)
      {
        my (\$a, \$b) = @_;

        return 0 if \$a == \$b;

        my @aver = split /[^[:alnum]]/, \$a;
        my @bver = split /[^[:alnum]]/, \$b;

        return 1 if @aver > @bver;
        return -1 if @aver < @bver;

        my \$cmp = 0;

        foreach my \$av (@aver)
          {
            my \$bv = shift @bver;

            if (\$av =~ /[^\d]/ || \$bv =~ /[^\d]/)
              {
                \$cmp = (\$av cmp \$bv);
              }
            else
              {
                \$cmp = (\$av <=> \$bv);
              }

            return \$cmp if \$cmp;
          }

        return 0;
      };

    if (\$ARGV[1] eq \"lt\")
      {
        exit ((rpmvercmp (\$ARGV[0], \$ARGV[2]) < 0) ? 0 : 1);
      }
    elsif (\$ARGV[1] eq \"le\")
      {
        exit ((rpmvercmp (\$ARGV[0], \$ARGV[2]) <= 0) ? 0 : 1);
      }
    elsif (\$ARGV[1] eq \"eq\")
      {
        exit ((rpmvercmp (\$ARGV[0], \$ARGV[2]) == 0) ? 0 : 1);
      }
    elsif (\$ARGV[1] eq \"ne\")
      {
        exit ((rpmvercmp (\$ARGV[0], \$ARGV[2]) != 0) ? 0 : 1);
      }
    elsif (\$ARGV[1] eq \"ge\")
      {
        exit ((rpmvercmp (\$ARGV[0], \$ARGV[2]) >= 0) ? 0 : 1);
      }
    elsif (\$ARGV[1] eq \"gt\")
      {
        exit ((rpmvercmp (\$ARGV[0], \$ARGV[2]) > 0) ? 0 : 1);
      }

    die \"bad arguments\";' \"\$@\" 
}
"

make_post_install () {
  cat <<EOD
#! /bin/sh

$comparescript

compare_versions=rpm_compare

canonicalize () {
  perl -e '(\$_) = @ARGV; s/-TEST1\$//; print \$_' -- "\$1"
}

$preamble

operation="\$1"

case "\$operation" in
  configure)
    most_recently_configured_version="\$2"
    canon_most_recently_configured_version="\`canonicalize \"\$most_recently_configured_version\"\`"
    $configure
    ;;
  abort-upgrade)
    new_version="\$2"
    canon_new_version="\`canonicalize \"\$new_version\"\`"
    $abort_upgrade
    ;;
  abort-remove)
    $abort_remove
    ;;
  *)
    ;;
esac

$postamble

exit 0
EOD
}

make_post_remove () {
  cat <<EOD
#! /bin/sh

$comparescript

compare_versions=rpm_compare

case "\$1" in
  upgrade)
    exit 1 # defer to new package's version
    ;;
  *)
    ;;
esac

canonicalize () {
  perl -e '(\$_) = @ARGV; s/-TEST1\$//; print \$_' -- "\$1"
}

$preamble

operation="\$1"

case "\$operation" in
  remove)
    $remove
    ;;
  purge)
    $purge
    ;;
  failed-upgrade)
    old_version="\$2"
    canon_old_version="\`canonicalize \"\$old_version\"\`"
    $upgrade
    ;;
  abort-install)
    old_version="\$2"
    canon_old_version="\`canonicalize \"\$old_version\"\`"
    $abort_install
    ;;
  abort-upgrade)
    old_version="\$2"
    canon_old_version="\`canonicalize \"\$old_version\"\`"
    $abort_upgrade
    ;;
  disappear)
    $remove
    ;;
  *)
    ;;
esac

$postamble

exit 0
EOD
}

make_pre_install () {
  cat <<EOD
#! /bin/sh

$comparescript

compare_versions=rpm_compare

canonicalize () {
  perl -e '(\$_) = @ARGV; s/-TEST1\$//; print \$_' -- "\$1"
}

$preamble

operation="\$1"

case "\$operation" in
  install)
    old_version="\$2"
    canon_old_version="\`canonicalize \"\$old_version\"\`"
    $install
    ;;
  upgrade)
    old_version="\$2"
    canon_old_version="\`canonicalize \"\$old_version\"\`"
    $upgrade
    ;;
  abort-upgrade)
    new_version="\$2"
    canon_new_version="\`canonicalize \"\$new_version\"\`"
    $abort_upgrade
    ;;
  *)
    ;;
esac

$postamble

exit 0
EOD
}

make_pre_remove () {
  cat <<EOD
#! /bin/sh

$comparescript

compare_versions=rpm_compare

case "\$1" in
  upgrade)
    exit 1 # defer to new package's version
    ;;
  *)
    ;;
esac

canonicalize () {
  perl -e '(\$_) = @ARGV; s/-TEST1\$//; print \$_' -- "\$1"
}

$preamble

operation="\$1"

case "\$operation" in
  remove)
    $remove
    ;;
  failed-upgrade)
    old_version="\$2"
    canon_old_version="\`canonicalize \"\$old_version\"\`"
    $upgrade
    ;;
  deconfigure)
    $deconfigure
    ;;
  *)
    ;;
esac

$postamble

exit 0
EOD
}

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "package/rpm/make-hook" "$@"

validate_args "package/rpm/make-hook" "hook" ""

case "$hook" in
  post-install)
    make_post_install
    ;;
  post-remove)
    make_post_remove
    ;;
  pre-install)
    make_pre_install
    ;;
  pre-remove)
    make_pre_remove
    ;;
  *)
    echo "package/rpm/make-hook: warning: unsupported hook '$hook'" 1>&2
    exit 0
esac
